package com.easyfun.healthmagicbox.widget;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;

import com.easyfun.healthmagicbox.R;
import com.easyfun.healthmagicbox.SubDocoterSuggestionActivity.ViewHolder;
import com.easyfun.healthmagicbox.pojo.ConstantData;
import com.easyfun.healthmagicbox.util.CustomDialogAdapter;
import com.easyfun.healthmagicbox.util.CustomDialogAdapter.ViewContainer;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class CustomDialogTemplate extends Activity {
	
	private boolean mMultiChoiceFlag = false; //multichoice is disabled by default.
	private ListView mDataListView = null;
	private CustomDialogAdapter mData = null;
	private String mTitle = "";
	private ArrayList<Boolean> mSelected = new ArrayList<Boolean>();
	
	public interface OnResult {
		public void onResult(int requestCode, int resultCode, Intent data);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.custom_dialog_template);
		
		Intent in = this.getIntent();
		String[] adapterData = in.getExtras().getStringArray(ConstantData.CUSTOM_DIALOG_INPUT_DATA);
		mTitle  		 = in.getExtras().getString(ConstantData.CUSTOM_DIALOG_TITLE);
		
	
		mData = new CustomDialogAdapter(this, adapterData);
		mDataListView        = (ListView) findViewById(R.id.custom_dialog_list_view);
		mDataListView.setAdapter(mData);
		
		
		/*multichoice?*/
		mMultiChoiceFlag     = in.getExtras().getBoolean(ConstantData.CUSTOM_DIALOG_MULIT_CHOICE_FLAG);
		if(mMultiChoiceFlag) {
			mDataListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
			//enable checkbox and select button
			Button selectBtn = (Button)findViewById(R.id.custom_dialog_button_select);
			selectBtn.setVisibility(View.VISIBLE);
			selectBtn.setOnClickListener(mSelectButtonClickListener);
		} else{
			mDataListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
			Button selectBtn = (Button)findViewById(R.id.custom_dialog_button_select);
			selectBtn.setVisibility(View.GONE);
		}
		mDataListView.setOnItemClickListener(mSingleItemClickListener); 
		TextView title       = (TextView) findViewById(R.id.custom_dialog_title);
		title.setText(mTitle);
		
		//update selected
		for(int i = 0; i < mData.getCount(); i ++) {
			mSelected.add(false); 
		}
		
		
	}

	private OnClickListener mSelectButtonClickListener = new OnClickListener() {
		public void onClick(View v) {
			SparseBooleanArray selectedItems = ((ListView)mDataListView).getCheckedItemPositions();
			for(int i = 0; i < selectedItems.size(); i ++) {
				mSelected.add(selectedItems.keyAt(i), selectedItems.valueAt(i));
			} 
			Intent intent = new Intent();
			intent.putExtra(ConstantData.CUSTOM_DIALOG_RETURN_DATA, mSelected);
			setResult(Activity.RESULT_OK, intent);
			finish();
		}
	};
	
	private OnItemClickListener mSingleItemClickListener = new OnItemClickListener()
	{
		public void onItemClick(AdapterView<?> av, View v, int selected, long arg3)
		{			
			// Create results
			if(mMultiChoiceFlag) {
                ViewContainer holder = (ViewContainer) v.getTag();  
                holder.cb.toggle();  
                mData.getIsSelected().put(selected, holder.cb.isChecked());  
                mData.notifyDataSetChanged();
				return;
			}
			Intent intent = new Intent();
			mSelected.add(selected, true);		
			
			// Set result and finish this Activity
			intent.putExtra(ConstantData.CUSTOM_DIALOG_RETURN_DATA, mSelected);
			setResult(Activity.RESULT_OK, intent);
			finish();
		}
	};	
}
